package example.com.zyfypt_618zgm.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.SortedList;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import retrofit2.Call;
import android.widget.Toast;

import java.util.List;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.adapter.FocusAdapter;
import example.com.zyfypt_618zgm.bean.FocusResult;
import example.com.zyfypt_618zgm.bean.UserBean;
import example.com.zyfypt_618zgm.model.ListFocusModel;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class Focus extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private FocusAdapter focusAdapter;
    private List<FocusResult<UserBean>> list=null;
    private int cur_page=1;
    private int lastVisibleItemPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclefocus);
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        manager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        focusAdapter=new FocusAdapter(this);
        focusAdapter.setList(list);
        recyclerView.setAdapter(focusAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    cur_page+=1;
                    Call<List<FocusResult<UserBean>>> call= new ListFocusModel().getFocusUserList("user", cur_page, Denglu.SessionID);
                    call.enqueue(new Callback<List<FocusResult<UserBean>>>(){
                        @Override
                        public void onResponse(Call<List<FocusResult<UserBean>>> call, Response<List<FocusResult<UserBean>>> response) {
                            if(response.isSuccessful()&&response.body()!=null){
                                list.addAll(response.body());
                                focusAdapter.setList(list);
                            }else{
                                Toast.makeText(Focus.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<FocusResult<UserBean>>> call, Throwable t) {
                            Toast.makeText(Focus.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
        Call<List<FocusResult<UserBean>>> call= new ListFocusModel().getFocusUserList("user", cur_page, Denglu.SessionID);
        call.enqueue(new Callback<List<FocusResult<UserBean>>>(){
            @Override
            public void onResponse(Call<List<FocusResult<UserBean>>> call, Response<List<FocusResult<UserBean>>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    if(list==null)
                        Toast.makeText(Focus.this,"您还没有关注用户",Toast.LENGTH_SHORT).show();
                    focusAdapter.setList(list);
                }else{
                    Toast.makeText(Focus.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<FocusResult<UserBean>>> call, Throwable t) {
                Toast.makeText(Focus.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
